<template>
	<view class="sidebar-menu">
		<!-- 菜单头部 -->
		<view class="menu-header">
			<text class="title">{{`云仓集配-${menuList.sys}`}}</text>
		</view>

		<!-- 菜单列表 -->
		<view class="menu-list">
			<!-- 一级菜单 -->
			<view class="menu-item" v-for="(item, index) in menuList.menu" :key="item.id">
				<view class="menu-title" @click="toggleSubMenu(index)">
					<view class="title-content">
						<text class="menu-icon" :class="item.icon"></text>
						<text class="menu-text">{{ item.title }}</text>
					</view>
					<text class="menu-arrow" :class="{ 'rotate-90': index===currentIndex }">
						<i class="fa fa-angle-right"></i>
					</text>
				</view>

				<!-- 二级菜单 -->
				<view class="sub-menu" v-if="currentIndex===index" :style="{ maxHeight: index===currentIndex ? '500px' : '0' }">
					<view class="sub-item" v-for="subItem in item.children" :key="subItem.id" @click="selecMenu(subItem.component)">
						{{ subItem.title }}
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
import { menu_wms, menu_ums,menu_operator } from '../untils/menu_list';
export default {
	name: 'SidebarMenu',
	props: {
		module: {
			type: String,
			default: ''
		}
	},
	data() {
		return {
			currentIndex:-1,
			sys:""
			
		};
	},
	computed: {
		menuList() {
			// 使用对象映射替代多重条件判断
			const menuMap = {
				wms:{menu:menu_wms,sys:'仓库WMS'},
				ums: {menu:menu_ums,sys:"货主UMS"},
				operator:{menu:menu_operator,sys:"运营商OEM"} 
			};

			// 返回对应模块的菜单或默认菜单
			return menuMap[this.module] || menu_default;
		}
	},
	methods: {
		// 切换子菜单展开/收起
		toggleSubMenu(index) {
			if(this.currentIndex===index){
				this.currentIndex=-1
			
			}else{
				  this.currentIndex=index
			}
 
		},
		selecMenu(component) {
			
			uni.$emit('open', component);
		},

	}
};
</script>

<style lang="scss" scoped>
// 定义颜色变量
$sidebar-bg: #1e293b;
$sidebar-text: #fff;
$sidebar-hover: #334155;
$border-color: #334155;
$transition-time: 0.3s;

.sidebar-menu {
	height: 100vh;
	background-color: $sidebar-bg;
	color: $sidebar-text;
	width: 100%;
	box-shadow: 0 0 15px rgba(0, 0, 0, 0.1);
	transition: all $transition-time ease-in-out;

	.menu-header {
		padding: 1rem;
		border-bottom: 1px solid $border-color;

		.title {
			font-size: 1.25rem;
			font-weight: bold;
		}
	}

	.menu-list {
		padding-top: 0.5rem;
		padding-bottom: 0.5rem;
		overflow-y: auto;
		height: calc(100% - 56px);

		.menu-item {
			.menu-title {
				display: flex;
				align-items: center;
				justify-content: space-between;
				padding-left: 1rem;
				padding-right: 1rem;
				padding-top: 0.75rem;
				padding-bottom: 0.75rem;
				cursor: pointer;
				transition: background-color $transition-time;

				&:hover {
					background-color: $sidebar-hover;
				}

				.title-content {
					display: flex;
					align-items: center;
					gap: 0.75rem;

					.menu-icon {
						font-size: 1.125rem;
					}

					.menu-text {
						font-weight: 500;
					}
				}

				.menu-arrow {
					transition: transform $transition-time;

					&.rotate-90 {
						transform: rotate(90deg);
					}
				}
			}

			.sub-menu {
				overflow: hidden;
				transition: max-height $transition-time ease-in-out;

				.sub-item {
					padding-left: 2.5rem;
					padding-top: 0.625rem;
					padding-bottom: 0.625rem;
					font-size: 0.875rem;
					cursor: pointer;
					transition: background-color $transition-time;

					&:hover {
						background-color: $sidebar-hover;
					}
				}
			}
		}
	}
}
</style>
